import requests
import json
 
url = "https://apimgateway.siemens-healthineers.com/openai/deployments/gpt-4o/chat/completions?api-version=2025-03-01-preview"
 
payload = json.dumps({
  "model": "chat",
  "messages": [
    {
      "role": "user",
      "content": "你好，你是哪家公司的?"
    }
  ]
})
headers = {
  'Content-Type': 'application/json',
  'api-key': "e102a51faae44d448f9687a437c541a5"
}
 
response = requests.request("POST", url, headers=headers, data=payload)
 
try:
    # 尝试将响应文本解析为JSON数据
    response_data = response.json()
    # 将解析后的数据保存到名为 response.json 的文件中
    with open('OpenAi/response.json', 'w', encoding='utf-8') as f:
        json.dump(response_data, f, ensure_ascii=False, indent=4)
    print("JSON文件已成功生成：response.json")
except ValueError:
    print("响应内容不是有效的JSON格式，无法生成文件。")
